package com.iot08.admin.controller;

import com.iot08.admin.controller.base.BaseController;
import com.iot08.admin.service.RefundApplyService;
import com.iot08.admin.service.page.PageVO;
import com.iot08.admin.service.page.PageableImpl;
import com.iot08.common.entity.admin.RefundApply;
import com.iot08.common.response.Response;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * @author cwj
 * @description
 * @date 2025/2/21 9:29
 **/
@Controller
@RequestMapping("refundOrder")
public class RefundApplyController extends BaseController {

    @Resource
    private RefundApplyService refundApplyService;
    @RequestMapping("list")
    public ModelAndView list(PageVO page,
                             @RequestParam(value = "startTime", required = false) String startTime,
                             @RequestParam(value = "endTime", required = false) String endTime,
                             @RequestParam(value = "transactionId",required = false) String transactionId,
                             @RequestParam(value = "outTradeNo",required = false) String outTradeNo,
                             @RequestParam(value = "refundType", required = false) String refundType,
                             @RequestParam(value = "orderType", required = false) Integer orderType,
                             @RequestParam(value = "productType", required = false) Integer productType) {
        ModelAndView mv = new ModelAndView();
        String id = page.getSearchValue();
        Page<RefundApply> pages = refundApplyService.list(page, startTime,endTime,outTradeNo,transactionId,refundType,orderType,productType);
        mv.addObject("refundOrders", pages.getContent());
        if (pages.getPageable() instanceof PageableImpl) {
            PageableImpl pi = (PageableImpl) pages.getPageable();
            mv.addObject("page", pi.getPage());
        }
        mv.addObject("refundType",refundType);
        mv.addObject("startTime",startTime);
        mv.addObject("endTime",endTime);
        mv.addObject("transactionId",transactionId);
        mv.addObject("outTradeNo",outTradeNo);
        mv.addObject("orderType",orderType);
        mv.addObject("productType",productType);
        mv.addObject("butSet", getMyBut(page.getMenuId()));
        mv.addObject("refundTotal",refundApplyService.queryRefundTotalByParam(id,startTime,endTime,outTradeNo,transactionId,refundType,orderType,productType));
        mv.setViewName("payorder/refundorder_list");
        return mv;
    }

    /**
     * @description 主动查询微信、支付宝、Apple的退款状态信息，更新退款订单的状态
     * @author cwj
     * @param [refundId]
     * @return com.iot08.common.response.Response<java.lang.Object>
     * @date @time 2025/2/10 14:03
     **/
    @ResponseBody
    @RequestMapping(value = "refundStatus", method = RequestMethod.POST)
    public Response<Object> queryRefundStatueInfo(String refundId) {
        if(StringUtils.isEmpty(refundId)){
            return Response.failedResponse("请求参数不能为空");
        }
        return refundApplyService.queryRefundStatueInfo(refundId);
    }

    /**
     * @description 导出数据
     * @author cwj
     * @param [refundId, startTime, endTime, transactionId, outTradeNo, refundType, response]
     * @return void
     * @date @time 2025/4/20 18:06
     **/
    @RequestMapping("import")
    public void importData(@RequestParam(value = "refundId", required = false) String refundId,
                           @RequestParam(value = "startTime", required = false) String startTime,
                           @RequestParam(value = "endTime", required = false) String endTime,
                           @RequestParam(value = "transactionId",required = false) String transactionId,
                           @RequestParam(value = "outTradeNo", required = false) String outTradeNo,
                           @RequestParam(value = "refundType", required = false) String refundType,
                           @RequestParam(value = "orderType", required = false) Integer orderType,
                           @RequestParam(value = "productType", required = false) Integer productType,
                           HttpServletResponse response)  {
        refundApplyService.importData(refundId,startTime,endTime,transactionId,outTradeNo,refundType,orderType,productType,response);
    }

    /**
     * @description 更新退款订单的订单类型及产品类型信息
     * @author cwj
     * @param []
     * @return com.iot08.common.response.Response<java.lang.Object>
     * @date @time 2025/5/1 10:04
     **/
    @RequestMapping("update")
    public Response<Object> updateRefundApplyOrderType(){
        return refundApplyService.updateRefundApplyOrderType();
    }

    /**
     * @description 查询退款总金额
     * @author cwj
     * @param []
     * @return com.iot08.common.response.Response<java.lang.Object>
     * @date @time 2025/5/14 11:42
     **/
    @ResponseBody
    @RequestMapping(value = "queryRefundTotal", method = RequestMethod.POST)
    public Response<Object> queryRefundTotal(){
        return refundApplyService.queryRefundTotal();
    }


}
